package com.guowen.myaccount;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.guowen.myaccount.adapter.AccountAdapter;
import com.guowen.myaccount.db.AccountBean;
import com.guowen.myaccount.db.DBManager;
import com.guowen.myaccount.db.MenuBean;
import com.guowen.myaccount.utils.ChoiceDialog;
import com.guowen.myaccount.utils.MenuDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ListView todayLv;

    private ImageView menuIv;
    private ImageView recordIv;
    private List<MenuBean> mDatas1, mDatas2;
    private static int year, month, day, week;

    //头布局相关控件
    private View headerView;
    private static TextView topMonthOutTv, topMonthInTv, topSumTv;
    private static TextView topYMDTimeTv, topWeekTimeTv, topDayOutTv, topDayInTv;
    private ImageView topShowIv;
    private SharedPreferences preferences;

    public AccountAdapter adapter;
    private List<AccountBean> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initTime();
        //添加ListView的头布局
        addLVHeaderView();
        mDatas = new ArrayList<>();
        adapter = new AccountAdapter(this, mDatas);
        todayLv.setAdapter(adapter);

        menuIv.setOnClickListener(this);
        recordIv.setOnClickListener(this);

        // 设置ListView单项监听
        addLVItemListener();

    }

    private void addLVItemListener() {
        todayLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    return false;
                }
                // 弹出对话框
                ChoiceDialog choiceDialog = new ChoiceDialog(MainActivity.this,position, mDatas, adapter);
                choiceDialog.show();

                return false;
            }
        });
    }


    private void initView() {
        menuIv = findViewById(R.id.main_top_iv);
        recordIv = findViewById(R.id.main_iv_more);
        todayLv = findViewById(R.id.main_lv);
    }

    //当Activity获取焦点时，会调用的方法
    @Override
    protected void onResume() {
        super.onResume();
        loadDBData();
        setTopTvShow();
    }

    //设置头布局当中文本的显示
    public static void setTopTvShow() {
        //获取今日总收入总支出的内容
        float incomeOneDay = DBManager.getSumMoneyOneDay(year, month, day, 1);
        float outcomeOneDay = DBManager.getSumMoneyOneDay(year, month, day, 0);

        //获取本月总收入总支出金额
        float incomeOneMonth = DBManager.getSumMoneyOneMonth(year, month, 1);
        float outcomeOneMonth = DBManager.getSumMoneyOneMonth(year, month, 0);
        topMonthInTv.setText("￥ "+incomeOneMonth);
        topMonthOutTv.setText("￥ "+outcomeOneMonth);
        topSumTv.setText("￥ "+DBManager.getSumMoneyAll());

        topYMDTimeTv.setText(year+"-"+month+"-"+day);
        topWeekTimeTv.setText(tranToStrWeek(week));
        topDayOutTv.setText(outcomeOneDay+"");
        topDayInTv.setText(incomeOneDay+"");

    }

    private static String tranToStrWeek(int week) {
        String Week = "";
        switch (week) {
            case 1:
                Week += "周日";
                break;
            case 2:
                Week += "周一";
                break;
            case 3:
                Week += "周二";
                break;
            case 4:
                Week += "周三";
                break;
            case 5:
                Week += "周四";
                break;
            case 6:
                Week += "周五";
                break;
            case 7:
                Week += "周六";
                break;
        }
        return Week;
    }

    private void loadDBData() {
        // 初始化菜单数据源
        mDatas1 = DBManager.getMenuBean(0);
        mDatas2 = DBManager.getMenuBean(1);
        // 初始化帐户行数据源
        List<AccountBean> list = DBManager.getAccountListOneDayFromAccounttb(year, month, day);
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_top_iv:
                MenuDialog menuDialog = new MenuDialog(this,mDatas1,mDatas2);
                menuDialog.show();
                menuDialog.setDialogSize();
                break;
            case R.id.main_iv_more:
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);
                startActivity(intent);
                break;
            case R.id.item_mainlv_top_iv_hide:
                //切换TextView明文和密文
                toggleShow();
                break;
        }
    }

    boolean isShow = true;
    // 明文和密文之间的转换方法
    private void toggleShow() {
        if(isShow){
            PasswordTransformationMethod instance = PasswordTransformationMethod.getInstance();
            topMonthInTv.setTransformationMethod(instance);  //设置隐藏
            topMonthOutTv.setTransformationMethod(instance);
            topSumTv.setTransformationMethod(instance);
            topShowIv.setImageResource(R.mipmap.hide);
            isShow = false;
        }else {
            HideReturnsTransformationMethod instance = HideReturnsTransformationMethod.getInstance();
            topMonthInTv.setTransformationMethod(instance);  //设置隐藏
            topMonthOutTv.setTransformationMethod(instance);
            topSumTv.setTransformationMethod(instance);
            topShowIv.setImageResource(R.mipmap.show);
            isShow = true;
        }
    }

    //给ListView添加头布局的方法
    private void addLVHeaderView() {
        headerView = getLayoutInflater().inflate(R.layout.item_mainlv_top,null);
        //查找头布局的可用控件
        topSumTv = headerView.findViewById(R.id.item_mainlv_top_tv_sum_num);
        topMonthInTv = headerView.findViewById(R.id.item_mainlv_top_tv_in);
        topMonthOutTv = headerView.findViewById(R.id.item_mainlv_top_tv_out);
        topShowIv = headerView.findViewById(R.id.item_mainlv_top_iv_hide);
        topYMDTimeTv = headerView.findViewById(R.id.item_main_time_YMD);
        topWeekTimeTv = headerView.findViewById(R.id.item_main_time_week);
        topDayOutTv = headerView.findViewById(R.id.item_main_time_out_num);
        topDayInTv = headerView.findViewById(R.id.item_main_time_in_num);
        //设置ListView的头布局
        todayLv.addHeaderView(headerView);

        topShowIv.setOnClickListener(this);

//        topSumTv.setOnClickListener(this);
//        headerView.setOnClickListener(this);
//        topShowIv.setOnClickListener(this);
    }

    //获取今日的具体时间
    private void initTime() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
        week = calendar.get(Calendar.DAY_OF_WEEK);
    }
}